package weixin.utils;


public class MessageUtils
{
    /**
     * 2016年8月6日
     * 
     * @param access_token 是 调用接口凭证 
     * @param userid   用户userid，多个用|分割 ，非必须
     * @param branchid 部门id ，多个用|分割，             非必须
     * @param agentid  企业应用id  必须
     * @param branchid 是 获取的部门id
     * @param content 消息内容，必须  
     * @return
     * @note:获取部门成员
     */
    public static String sendText(String access_token,String userid,String branchid,String agentid,String content){
        String url="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="+access_token;
        String data="{"+
                       "\"touser\": \""+userid+"\","+ //用户userid，多个用     | 分割 ，非必须
                       "\"toparty\": \""+branchid+"\","+       //部门id ，多个用|  分割，非必须
                       "\"totag\": \"@all\","+      //标签ID列表，多个接收者用‘|’分隔。当touser为@all时忽略本参数,非必须
                       "\"msgtype\": \"text\","+    //消息推送了类型
                       "\"agentid\": "+agentid+","+           //企业应用id
                       "\"text\": {"+
                          "\"content\": \""+content+"\""+   //content ，消息体
                       "},"+
                       "\"safe\":\"0\""+            //表示是否是保密消息，0表示否，1表示是，默认0        非必须
                    "}";
        String result=RequestUtils.doPost(url, data);
        return result;
    }
    
    
    //发送消息
    public static String sendMessage(String access_token,String type){
        String url="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="+access_token;
        String data=null;//推送消息的body
        switch (type) {
        case "text":
             data="{"+
                       "\"touser\": \"zhangsan\","+ //用户userid，多个用     | 分割 ，非必须
//                     "\"toparty\": \"1\","+       //部门id ，多个用|  分割，非必须
                       "\"totag\": \"@all\","+      //标签ID列表，多个接收者用‘|’分隔。当touser为@all时忽略本参数,非必须
                       "\"msgtype\": \"text\","+    //消息推送了类型
                       "\"agentid\": 1,"+           //企业应用id
                       "\"text\": {"+
                          "\"content\": \"企业消息，单一消息推送\""+   //content ，消息体
                       "},"+
                       "\"safe\":\"0\""+            //表示是否是保密消息，0表示否，1表示是，默认0        非必须
                    "}";
            break;
        case "image":
            data="{"+
                       "\"touser\": \"zhuxiao\","+ //用户userid，多个用     | 分割 ，非必须
//                     "\"toparty\": \"1\","+       //部门id ，多个用|  分割，非必须
                       "\"totag\": \"@all\","+      //标签ID列表，多个接收者用‘|’分隔。当touser为@all时忽略本参数,非必须
                       "\"msgtype\": \"image\","+   //消息推送了类型
                       "\"agentid\": 1,"+           //企业应用id
                       "\"image\": {"+
                           "\"media_id\": \"1bO_Gb-X12pP018UPXsKo4-grwhXQRR-G5TdY_qYxPNjvDEghh68DH8JRQ5fqRNN35Kk8v_JUZkmMqWgy8tPSng\""+
                       "},"+
                       "\"safe\":\"0\""+            //表示是否是保密消息，0表示否，1表示是，默认0        非必须
                    "}";
            break;
            
        case "voice": //语音消息
            data="{"+
                       "\"touser\": \"zhuxiao\","+ //用户userid，多个用     | 分割 ，非必须
//                     "\"toparty\": \"1\","+       //部门id ，多个用|  分割，非必须
                       "\"totag\": \"@all\","+      //标签ID列表，多个接收者用‘|’分隔。当touser为@all时忽略本参数,非必须
                       "\"msgtype\": \"voice\","+   //消息推送了类型
                       "\"agentid\": 1,"+           //企业应用id
                       "\"voice\": {"+
                           "\"media_id\": \"1dTVnmGQsOQJ_IQYf8z12bpThu3_jZ8PIh2jmxAopMG9SD8MhPT2IcDrNFVBjgPClS0Xo5ATb3Xdap4LC9ItZmg\""+
                       "},"+
                       "\"safe\":\"0\""+            //表示是否是保密消息，0表示否，1表示是，默认0        非必须
                    "}";
            break;
        
        case "video": //视频消息
            data="{"+
                       "\"touser\": \"zhuxiao\","+ //用户userid，多个用     | 分割 ，非必须
//                     "\"toparty\": \"1\","+       //部门id ，多个用|  分割，非必须
                       "\"totag\": \"@all\","+      //标签ID列表，多个接收者用‘|’分隔。当touser为@all时忽略本参数,非必须
                       "\"msgtype\": \"video\","+   //消息推送了类型
                       "\"agentid\": 1,"+           //企业应用id
                       "\"video\": {"+
                           "\"media_id\": \"1U3GW0tHMeI8hIzvhYugjmv4fHHRsB90t6gYKLuogbF6NJwTJR4Smo1xGFsPq5sYUT22bFMnbT_QS90BkrqLeWA\","+
                           "\"title\": \"标题\","+
                           "\"description\": \"描述\""+
                       "},"+
                       "\"safe\":\"0\""+            //表示是否是保密消息，0表示否，1表示是，默认0        非必须
                    "}";
            break;  
        
        case "file": //语音消息
            data="{"+
                       "\"touser\": \"zhuxiao\","+ //用户userid，多个用     | 分割 ，非必须
//                     "\"toparty\": \"1\","+       //部门id ，多个用|  分割，非必须
                       "\"totag\": \"@all\","+      //标签ID列表，多个接收者用‘|’分隔。当touser为@all时忽略本参数,非必须
                       "\"msgtype\": \"file\","+    //消息推送了类型
                       "\"agentid\": 1,"+           //企业应用id
                       "\"file\": {"+
                           "\"media_id\": \"193Ov6lGQNEb8RpJXA4SJc2ZxHThyclpzIYb3eJw0zEIZGajAS9taa7Bv8tdjEvfLn2dFs5JuTSk1lJDhi_rG-A\""+
                       "},"+
                       "\"safe\":\"0\""+            //表示是否是保密消息，0表示否，1表示是，默认0        非必须
                    "}";
            break;  
        
        case "news": //
            data="{"+
                    "\"touser\": \"zhuxiao\","+
                    "\"totag\": \"@all\","+
                    "\"msgtype\": \"news\","+
                    "\"agentid\": 1,"+
                    "\"news\": {"+
                        "\"articles\": ["+
                            "{"+
                                "\"title\": \"标题1\","+
                                "\"description\": \"描述1\","+
                                "\"url\": \"http://qyh.lgh.faith:7777/qyh/wx/accessToken.zx\","+
                                "\"picurl\": \"http://qyh.lgh.faith:7777/qyh/img/t1.jpg\""+
                            "},"+
                            "{"+
                                "\"title\": \"标题2\","+
                                "\"description\": \"描述2\","+
                                "\"url\": \"http://qyh.lgh.faith:7777/qyh/wx/accessToken.zx\","+
                                "\"picurl\": \"http://qyh.lgh.faith:7777/qyh/img/t2.jpg\""+
                           "}"+
                        "]"+
                    "}"+
                "}";
            break;      
        
        case "mpnews.A": //
            String content1="测试测试测试测试测试测测试测试测试测试测试测试测试<input value=\"提交\" type=\"sumit\"/>";
            String content2="22222222<br>测试测试测试测试测试测试测试测试测试测试测试测试测试测试<input value=\"提交\" type=\"sumit\"/>";
            data="{"+
                       "\"touser\": \"zhuxiao\","+
                       "\"toparty\": \"@all\","+
                       "\"totag\": \"@all\","+
                       "\"msgtype\": \"mpnews\","+
                       "\"agentid\": 1,"+
                       "\"mpnews\": {"+
                           "\"articles\":["+
                               "{"+
                                   "\"title\": \"标题1\","+
                                   "\"thumb_media_id\": \"1bO_Gb-X12pP018UPXsKo4-grwhXQRR-G5TdY_qYxPNjvDEghh68DH8JRQ5fqRNN35Kk8v_JUZkmMqWgy8tPSng\","+      //图文消息缩略图的media_id, 可以在上传多媒体文件接口中获得。此处thumb_media_id即上传接口返回的media_id
                                   "\"author\": \"作者\","+              //图文消息的作者，不超过64个字节
                                   "\"content_source_url\": \"http://qyh.lgh.faith:7777/qyh/wx/accessToken.zx\","+ //图文消息点击“阅读原文”之后的页面链接
                                   "\"content\": \""+content1+"\","+           //图文消息的内容，支持html标签，不超过666 K个字节
                                   "\"digest\": \"图文消息的描述，不超过512个字节，超过会自动截断\","+ //图文消息的描述，不超过512个字节，超过会自动截断
                                   "\"show_cover_pic\": \"0\""+         //是否显示封面，1为显示，0为不显示
                               "},"+
                               "{"+
                                   "\"title\": \"Title\","+
                                   "\"thumb_media_id\": \"1bO_Gb-X12pP018UPXsKo4-grwhXQRR-G5TdY_qYxPNjvDEghh68DH8JRQ5fqRNN35Kk8v_JUZkmMqWgy8tPSng\","+
                                   "\"author\": \"Author\","+
                                   "\"content_source_url\": \"http://qyh.lgh.faith:7777/qyh/wx/accessToken.zx\","+
                                   "\"content\": \""+content2+"\","+
                                   "\"digest\": \"图文消息的描述，不超过512个字节，超过会自动截断\","+
                                   "\"show_cover_pic\": \"0\""+
                               "}"+
                           "]"+
                       "},"+
                       "\"safe\":\"0\""+
                    "}";
            break;  
        case "mpnews.B": //
            data="{"+
                   "\"touser\": \"朱笑\"," +
                   "\"toparty\": \"1\", "+
                   "\"msgtype\": \"mpnews\", "+
                   "\"agentid\": 1, "+
                   "\"mpnews\": {"+
                       "\"media_id\": \"MEDIA_ID\""+
                   "},"+
                   "\"safe\": \"0\""+
                "}";
            break;  
            
        default:
            break;
        }
        
        String result=RequestUtils.doPost(url, data);
        return result;

    }
}
